package com.hdyg.sixg.activity.home;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.DigitalCashBean;
import com.hdyg.sixg.mvp.Contrant.CDigitalCash;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PDigitalCash;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 数字钱包提现
 * @time 2019/4/9 10:14
 */
public class DigitalCashActivity extends BaseActivity implements CDigitalCash.IVDigitalCash {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_balance)
    SuperTextView svBalance;
    @BindView(R.id.sv_bank_card)
    SuperTextView svBankCard;
    @BindView(R.id.et_cash_number)
    EditText etCashNumber;
    @BindView(R.id.sv_6g)
    SuperTextView sv6g;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.tv_fee_des)
    TextView tvFeeDes;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.ll_cash_money)
    LinearLayout llCashMoney;

    private String biType = ""; //eg: btc
    private String type;    //  请求参数需要类型
    private CDigitalCash.IPDigitalCash mPresenter;
    private String amount, trapass;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash;
    }

    @Override
    protected void initView() {
        onClickListener();
        biType = getIntent().getStringExtra(SpMsg.INTENT_STR);
        type = getIntent().getStringExtra(SpMsg.INTENT_CODE);
        String title = String.format(getResources().getString(R.string.wallet_title), biType);
        tvTopTitle.setText(title);
        String str = String.format(getResources().getString(R.string.wallet_use), biType);
        svBalance.setLeftString(str);
        svBankCard.setLeftTopString(getResources().getString(R.string.wallet_cash_address));
        tvNumber.setText(getResources().getString(R.string.wallet_cash_number));
        etCashNumber.setHint(getResources().getString(R.string.cash_money_hint));
        sv6g.setVisibility(View.GONE);
        svFee.setVisibility(View.GONE);
        tvFeeDes.setVisibility(View.GONE);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());

        btSure.setOnClickListener(v -> {
            amount = etCashNumber.getText().toString().trim();
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.cash_number_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyCash(BaseUrl.APPLY_DIGITAL_CASH_URL, GetParamUtil.getApplyDigitalCashParam(type, amount, trapass));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.DIGITAL_CASH_INIT_URL, GetParamUtil.getDigitalCashParam(type));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PDigitalCash(this);
    }

    @Override
    public void vGetInitSuccess(DigitalCashBean dataBean) {
        svBankCard.setLeftBottomString(dataBean.getWalletaddr());
        svBalance.setRightString(dataBean.getBalance());
    }

    @Override
    public void vGetApplyCashSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
